﻿using System;
using System.IO;
using Microsoft.Xna.Framework.Audio;
using MP3Sharp;

namespace Terraria.ModLoader.Audio
{
	// Token: 0x02000274 RID: 628
	public abstract class MusicStreaming : Music, IDisposable
	{
		// Token: 0x170001C0 RID: 448
		// (get) Token: 0x060019E0 RID: 6624 RVA: 0x000124E0 File Offset: 0x000106E0
		public override bool IsDisposed
		{
			get
			{
				return this.instance.IsDisposed;
			}
		}

		// Token: 0x170001C1 RID: 449
		// (get) Token: 0x060019E1 RID: 6625 RVA: 0x000124ED File Offset: 0x000106ED
		public override bool IsPaused
		{
			get
			{
				return this.instance.State == SoundState.Paused;
			}
		}

		// Token: 0x170001C2 RID: 450
		// (get) Token: 0x060019E2 RID: 6626 RVA: 0x000124FD File Offset: 0x000106FD
		public override bool IsPlaying
		{
			get
			{
				return this.instance.State != SoundState.Stopped;
			}
		}

		// Token: 0x060019E3 RID: 6627 RVA: 0x00012510 File Offset: 0x00010710
		public override void Pause()
		{
			this.instance.Pause();
		}

		// Token: 0x060019E4 RID: 6628 RVA: 0x0001251D File Offset: 0x0001071D
		public override void Play()
		{
			this.instance.Play();
		}

		// Token: 0x060019E5 RID: 6629 RVA: 0x0001252A File Offset: 0x0001072A
		public override void Resume()
		{
			this.instance.Resume();
		}

		// Token: 0x060019E6 RID: 6630 RVA: 0x00426ED4 File Offset: 0x004250D4
		public override void SetVariable(string name, float value)
		{
			if (name == "Volume")
			{
				this.instance.Volume = value;
				return;
			}
			if (name == "Pitch")
			{
				this.instance.Pitch = value;
				return;
			}
			if (!(name == "Pan"))
			{
				throw new Exception("Invalid field: '" + name + "'");
			}
			this.instance.Pan = value;
		}

		// Token: 0x060019E7 RID: 6631 RVA: 0x00012537 File Offset: 0x00010737
		public override void Stop(AudioStopOptions options)
		{
			this.instance.Stop();
		}

		// Token: 0x060019E8 RID: 6632 RVA: 0x00012544 File Offset: 0x00010744
		public override void CheckBuffer()
		{
			if (this.instance.PendingBufferCount < 3 && this.IsPlaying)
			{
				this.SubmitBuffer(3);
			}
		}

		// Token: 0x060019E9 RID: 6633 RVA: 0x00426F48 File Offset: 0x00425148
		internal void SubmitBuffer(int count)
		{
			if (this.stream == null)
			{
				this.instance.Stop();
				return;
			}
			int i = 0;
			while (i < count)
			{
				if (!this.SubmitSingle())
				{
					if (i == 0)
					{
						this.instance.Stop();
						return;
					}
					break;
				}
				else
				{
					i++;
				}
			}
		}

		// Token: 0x060019EA RID: 6634 RVA: 0x00426F8C File Offset: 0x0042518C
		private bool SubmitSingle()
		{
			int num = this.stream.Read(MusicStreaming.buffer, 0, MusicStreaming.buffer.Length);
			if (num < 4096)
			{
				this.ResetStreamPosition();
				this.stream.Read(MusicStreaming.buffer, num, MusicStreaming.buffer.Length - num);
			}
			this.instance.SubmitBuffer(MusicStreaming.buffer);
			return true;
		}

		// Token: 0x060019EB RID: 6635 RVA: 0x00426FEC File Offset: 0x004251EC
		public void ResetStreamPosition()
		{
			if (this.stream != null)
			{
				this.stream.Position = 0L;
				MP3Stream mp3Stream = this.stream as MP3Stream;
				if (mp3Stream != null)
				{
					mp3Stream.IsEOF = false;
				}
			}
		}

		// Token: 0x060019EC RID: 6636 RVA: 0x00012563 File Offset: 0x00010763
		public void Dispose()
		{
			if (this.instance.State == SoundState.Playing)
			{
				this.instance.Stop();
			}
			this.instance.Dispose();
			this.instance = null;
			this.stream.Close();
			this.stream = null;
		}

		// Token: 0x04001749 RID: 5961
		private const int bufferMin = 3;

		// Token: 0x0400174A RID: 5962
		private const int bufferCountPerSubmit = 3;

		// Token: 0x0400174B RID: 5963
		internal const int DEFAULT_BYTESPERCHUNK = 4096;

		// Token: 0x0400174C RID: 5964
		internal static byte[] buffer = new byte[4096];

		// Token: 0x0400174D RID: 5965
		internal DynamicSoundEffectInstance instance;

		// Token: 0x0400174E RID: 5966
		internal Stream stream;

		// Token: 0x0400174F RID: 5967
		internal long dataStart;
	}
}
